import {
    get,
    post,
    put
} from '../../utils/request'
import {
    APIS
} from '../../utils/api'
Page({
    data: {
        id: undefined,
        type: 'recharge',
        rechargeKey: [{
                name: '充值方案',
                key: 'name'
            },
            {
                name: '起止时间',
                key: 'time'
            },
            {
                name: '有效时长',
                key: 'efficient'
            },
            {
                name: '使用门店',
                key: 'sname'
            },
            {
                name: '是否主推',
                key: 'mainuseCN'
            }
        ],
        discountKey: [{
                name: '优惠类型',
                key: 'alias'
            },
            {
                name: '优惠详情',
                key: 'alias'
            },
            {
                name: '起止时间',
                key: 'time'
            },
            {
                name: '有效时长',
                key: 'efficient'
            },
            {
                name: '适用门店',
                key: 'sname'
            },
            {
                name: '是否需要押金',
                key: 'depositCN'
            },
        ],
        title: [
            ['充值日期', '充值人', '联系方式', '充值金额', '会员余额'],
            ['领取日期', '领取人', '联系方式', '优惠券'],
        ],
        keys: [
            ['date', 'name', 'tel', 'pay', 'amount'],
            ['date', 'name', 'tel', 'alias']
        ],
        info: {},
        details: [],
        showTooltip: false
    },
    onLoad(options) {
        this.setData(options)
    },
    onShow() {
        this.getDetail();
    },

    getBaseUrl() {
        const {
            type,
            id
        } = this.data;
        const baseUrl = type === 'recharge' ?
            `${APIS.recharge}/${id}` :
            `${APIS.discount}/${id}`;
        return baseUrl;
    },

    getDetail() {
        const baseUrl = this.getBaseUrl();

        get(baseUrl).then(res => {
            let {
                data,
                details
            } = res.data;
            this.setData({
                details,
                info: {
                    ...data,
                    mainuseCN: ['否', '是'][data.mainuse],
                    depositCN: ['否', '是'][data.deposit],
                    time: `${data.start} ~ ${data.end}`
                }
            });
        })
    },
    handleEdit() {
        const {
            type,
            id
        } = this.data;
        wx.navigateTo({
            url: `/pages/rechargenew/index?type=${type}&action=modify&id=${id}`,
        })
    },

    handleBeforeDel() {
        this.setData({
            showTooltip: true
        })
    },
    handleCancel() {
        this.setData({
            showTooltip: false
        })
    },

    handleDel() {
        const baseUrl = this.getBaseUrl();
        put(baseUrl + '/delete').then(res => {
            if (res.code === 0) {
                wx.navigateBack(-1);
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })
    }
})